package com.yoso.starter.boot.util;

import lombok.SneakyThrows;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Objects;

/**
 * <p>
 * 权限工具
 * </p>
 *
 * @author shizejin
 * @version 1.0
 * @date 2023/2/27 11:03
 */
public class AuthUtil {

    /**
     * 获取用户编码
     *
     * @return java.lang.String
     * @author Michael Xu
     * @date 2023/2/28 10:51
     */
    @SneakyThrows(value = {UnsupportedEncodingException.class})
    public static String getUserCode() {
        String userCode = "";
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (Objects.nonNull(requestAttributes)) {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            if (request.getHeader("userCode") != null) {
                userCode = URLDecoder.decode(request.getHeader("userCode"), "UTF-8");
            }
        }
        return userCode;
    }

    /**
     * 获取用户名称
     *
     * @return java.lang.String
     * @author Michael Xu
     * @date 2023/2/28 10:51
     */
    @SneakyThrows(value = {UnsupportedEncodingException.class})
    public static String getUserName() {
        String userName = "";
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (Objects.nonNull(requestAttributes)) {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            if (request.getHeader("userName") != null) {
                userName = URLDecoder.decode(request.getHeader("userName"), "UTF-8");
            }
        }
        return userName;
    }


    /**
     * 获取用户id
     *
     * @return java.lang.String
     * @author Michael Xu
     * @date 2023/2/28 10:51
     */
    public static String getUserId() {
        String userId = "";
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (Objects.nonNull(requestAttributes)) {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            if (request.getHeader("userId") != null) {
                userId = request.getHeader("userId");
            }
        }
        return userId;
    }


    /**
     * 获取客户端请求ip
     *
     * @return java.lang.String
     * @author Michael Xu
     * @date 2023/5/8 19:50
     */
    public static String getClientIp() {
        String ip = "";
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (Objects.nonNull(requestAttributes)) {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            ip = IPUtil.getClientIp(request);
        }
        return ip;
    }
}